﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using LoginUser.PopUpWindows;
using Sync.AssignmentService;
using Sync.LMS;
using Sync.Log;
using Sync.SessionService;

namespace LoginUser
{
    /// <summary>
    /// Interaction logic for SplashUtilities.xaml
    /// </summary>
    public partial class SplashUtilities : Window
    {
        public static ClassChooser Chooser = new ClassChooser();
        public static FileSyncChooser FileChooser = new FileSyncChooser();
        public static AboutUs about = new AboutUs();

        //private Thread tickleThread;
        private List<TextBlock> _coursetextBlocks;

        public SplashUtilities()
        {
            InitializeComponent();
            ResizeMode = ResizeMode.CanMinimize;
            Chooser.Visibility = Visibility.Hidden;
            FileChooser.Visibility = Visibility.Hidden;
            about.Visibility = Visibility.Hidden;
        }
        private void CloseAllWindows()
        {
            for (int intCounter = Application.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
            {
                var window = Application.Current.Windows[intCounter];
                if (window != null)
                    window.Close();
            }
        }

        private void ViewChange_Click(object sender, RoutedEventArgs e)
        {
            FileLog.LogMessage(Path.GetFullPath(FileLog.LogPath));
            Process.Start(FileLog.LogPath);
        }
        
        private void File_Sync_Click(object sender, RoutedEventArgs e)
        {
            Visibility = Visibility.Hidden;
            FileChooser.ShowDialog();
        }
        private void About_Us_Click(object sender, RoutedEventArgs e)
        {
            Visibility = Visibility.Hidden;
            about.ShowDialog();
        }
        
        private void Class_Chooser_Click(object sender, RoutedEventArgs e)
        {
            Visibility = Visibility.Hidden;
            Chooser.ShowDialog();
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //FileLog.LogMessage("Tickling should start");
            //LMSServiceUsage.DoneTickling = false;
            //tickleThread = new Thread(new Tickler().StartTickles);
            //tickleThread.Start();

            UserNameTextBlock.Text = LMSServiceUsage.Name;
            var courseShortNames = new ObservableCollection<string>();
            var courses = LMSServiceUsage.GetCourses();
            
            var enumerator = courses.GetEnumerator();
            while(enumerator.MoveNext())
            {
                courseShortNames.Add((enumerator.Current).ShortName);
            }
            
            SetupTextblockList();
            PopulateCourses(courseShortNames);
            
        }

        

        private void SetupTextblockList()
        {
            _coursetextBlocks = new List<TextBlock>
                                    {
                                        Class1TextBlock,
                                        Class2TextBlock,
                                        Class3TextBlock,
                                        Class4TextBlock,
                                        Class5TextBlock,
                                        Class6TextBlock,
                                        Class7TextBlock,
                                        Class8TextBlock
                                    };
        }

        private void PopulateCourses(ObservableCollection<string> courses)
        {
            int i = 0;
            foreach (var course in courses)
            {
                _coursetextBlocks[i].Text = course;
                    i++;
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            using (var client = new SessionServicesClient())
            {
                try
                {
                    var logoutResponse = client.Logout(new LogoutRequest { SessionKey = LMSServiceUsage.MySessionKey });
                    MainWindow main = new MainWindow();
                    main.ShowDialog();
                    Visibility = Visibility.Hidden;
                    CloseAllWindows();
                }
                catch (Exception)
                {
                    FileLog.LogMessage("Error Logging out", LogType.Warning);
                    MessageBox.Show("There was an error while logging out.", "Logout Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            //LMSServiceUsage.DoneTickling = true;
        }

        private void Issue_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("http://code.google.com/p/lms-gui/issues/list");
        }
    }
}
